package com.ruoyi.cake.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cake.mapper.CakeMapper;
import com.ruoyi.cake.domain.Cake;
import com.ruoyi.cake.service.ICakeService;

/**
 * 蛋糕Service业务层处理
 *
 * @author zichen
 * @date 2024-10-22
 */
@Service
public class CakeServiceImpl implements ICakeService
{
    @Autowired
    private CakeMapper cakeMapper;

    /**
     * 查询蛋糕
     *
     * @param id 蛋糕主键
     * @return 蛋糕
     */
    @Override
    public Cake selectCakeById(Long id)
    {
        return cakeMapper.selectCakeById(id);
    }

    /**
     * 查询蛋糕列表
     *
     * @param cake 蛋糕
     * @return 蛋糕
     */
    @Override
    public List<Cake> selectCakeList(Cake cake)
    {
        return cakeMapper.selectCakeList(cake);
    }

    /**
     * 新增蛋糕
     *
     * @param cake 蛋糕
     * @return 结果
     */
    @Override
    public int insertCake(Cake cake)
    {
        cake.setCreateTime(DateUtils.getNowDate());
        cake.setStoreTime(DateUtils.getNowDate());
        if (cake.getCount() >= 0) {
            for (int i = 0; i < cake.getCount(); i++) {
                cakeMapper.insertCake(cake);
            }
            return 1;
        } else {
            return cakeMapper.insertCake(cake);
        }
    }

    /**
     * 修改蛋糕
     *
     * @param cake 蛋糕
     * @return 结果
     */
    @Override
    public int updateCake(Cake cake)
    {
        cake.setUpdateTime(DateUtils.getNowDate());
        return cakeMapper.updateCake(cake);
    }

    /**
     * 批量删除蛋糕
     *
     * @param ids 需要删除的蛋糕主键
     * @return 结果
     */
    @Override
    public int deleteCakeByIds(Long[] ids)
    {
        return cakeMapper.deleteCakeByIds(ids);
    }

    /**
     * 删除蛋糕信息
     *
     * @param id 蛋糕主键
     * @return 结果
     */
    @Override
    public int deleteCakeById(Long id)
    {
        return cakeMapper.deleteCakeById(id);
    }

    /**
     * 出库
     *
     * @param cake 蛋糕
     * @return 结果
     */
    @Override
    public int outbound(Cake cake) {
        //查询蛋糕
        List<Cake> cakes = cakeMapper.selectCakeList(cake);
        int count = cake.getCount();
        if (count >= 0 && count <= cakes.size()) {
            for (int i = 0; i < count; i++) {
                cakeMapper.deleteCakeById(cakes.get(i).getId());
            }
            return 1;
        } else {
            throw new ServiceException("出库失败！库存数量小于输入数量，库存不足");
        }
    }
}
